﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace AGuang.Script.TypeConvert {
    public class ArrayConverter:TypeConverter {
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
            if (value is String) {
                String text = (String)value;
                if (String.IsNullOrEmpty(text)) return base.ConvertFrom(context, culture, value);
                String[] list = text.Split(',');
                return list;
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
            if (destinationType == typeof(String)) {
                String[] list = (String[])value;
                return String.Join(",", list);
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
            if (sourceType == typeof(String)) return true;
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
            if (destinationType == typeof(String)) return true;
            return base.CanConvertTo(context, destinationType);
        }

    }
}
